﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for KhachHangDAO
/// </summary>
/// 
namespace Web_cuiki
{
    public class KhachHangDAO
    {

        public KhachHangDAO()
        {

        }
        public static List<KHACH_HANG> laydsKhachHang()
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            List<KHACH_HANG> ds = null;
            var query = from kh in db.KHACH_HANGs select kh;
            ds = query.ToList<KHACH_HANG>();
            return ds;
        }
        public static KHACH_HANG TimKhachHangTheoMa(string ten)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from kh in db.KHACH_HANGs where kh.TenDangNhap == ten select kh;
            KHACH_HANG kq = query.First<KHACH_HANG>();
            return kq;

        }
        public static KHACH_HANG TimKhachHangTheoTenDangNhap(string tenDN)
        {
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            var query = from kh in db.KHACH_HANGs where kh.TenKhachHang == tenDN select kh;
            KHACH_HANG kq = query.First<KHACH_HANG>();
            return kq;

        }

        public static string KiemTraDangNhap(string ten, string pass, string key)
        {
            string kq = "";
            //giai ma pass
            string passsaukhiDe = Util.GiaiMa(key, pass);
            //lay pass cua khach hang de so sanh
            KHACH_HANG kh = TimKhachHangTheoTenDangNhap(ten);
            if (passsaukhiDe == kh.MatKhau)
                kq = "OK";
            else
                kq = "Dang nhap that bai";
            return kq;
        }
        public static int ThemKhachHang(string tenDN, string tenKH, string matkhau, string diachi, string dienthoai)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KHACH_HANG> table = db.GetTable<KHACH_HANG>();
            try
            {
                KHACH_HANG kh = new KHACH_HANG();
                if (diachi != null)
                    kh.DiaChi = diachi;
                if (dienthoai != null)
                    kh.DienThoai = dienthoai;
                if (tenDN != null)
                    kh.TenDangNhap = tenDN;
                if (tenKH != null)
                    kh.TenKhachHang = tenKH;
                if (matkhau != null)
                    kh.MatKhau = matkhau;
                table.InsertOnSubmit(kh);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int SuaKhachHang(string tenDN, string tenKH, string matkhau, string diachi, string dienthoai)
        {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            System.Data.Linq.Table<KHACH_HANG> table = db.GetTable<KHACH_HANG>();
            var query = from kh in db.KHACH_HANGs where kh.TenDangNhap == tenDN select kh;
            KHACH_HANG khachhang = query.First<KHACH_HANG>();
            try
            {

                if (diachi != null)
                    khachhang.DiaChi = diachi;
                if (dienthoai != null)
                    khachhang.DienThoai = dienthoai;
                if (tenKH != null)
                    khachhang.TenKhachHang = tenKH;
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static int XoaKhachHangTheoTenDangNhap(string tenDN) {
            int kq = -1;
            NhaXuatBanDataContext db = new NhaXuatBanDataContext();
            try
            {
                var query = from hd in db.KHACH_HANGs where hd.TenDangNhap == tenDN select hd;
                KHACH_HANG hoadon = query.First<KHACH_HANG>();
                System.Data.Linq.Table<KHACH_HANG> table = db.GetTable<KHACH_HANG>();
                table.DeleteOnSubmit(hoadon);
                table.Context.SubmitChanges();
                kq = 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
    }
}